//index.js
const config = require('../../config.js')
const http = require('../../http.js')
//获取应用实例
const app = getApp()

var page = 1

Page({
  data: {
    bannerData: [],
    noticeData: [],
    announcementData: [],
    isShow:0,
    showModal: false,
    userAvatarUrl: "",
    userNickName: "未登录",
  },
  onShow: function () { 
    // var isLogin = wx.getStorageSync('isLogin')
    // if (!isLogin) {
    //   wx.navigateTo({
    //     url: '../../pages/login/login-default/login-default',
    //   })
    // } else {
      var userInfo = wx.getStorageSync('userInfo')
      console.log(userInfo)
      if (userInfo) {
        this.setData({
          userNickName:userInfo.name,
          isShow: 1,
        })
      } else {
        var user = app.globalData.userInfo
        console.log(userInfo)
        if (user != null) {
          this.setData({
            userNickName:userInfo.name,
            isShow: 1,
          })
        }
      // }
    }
  },


  onLoad: function() {
    var value = wx.getStorageSync('sessionId')
    if (value) {
      page = 1
      this.getBanners()
    }
    app.watch(this.watchBack)
  },

  onShareAppMessage: function () {
    // return custom share data when user share.
  },

  watchBack: function(value) {
    if (value){
      page = 1
      this.getBanners()
    }
   
  },

  navigationClick: function(e) {
    var isLogin = wx.getStorageSync('isLogin')
    if (!isLogin) {
      wx.navigateTo({
        url: '../../pages/login/login-default/login-default',
      })
    }
    console.log(e.currentTarget.dataset.pos)
    switch (e.currentTarget.dataset.pos) {
      case '0':
        wx.navigateTo({
          url: '/pages/course/course',
        })
        break
      case '1':
        wx.navigateTo({
          url: '/pages/exam/exam',
        })
        break
      case '2':
        wx.navigateTo({
          url: '../../pages/user/ranking-list/ranking-list',
        })
        break
      case '3':
        wx.navigateTo({
          url: '/pages/mine/mine',
        })
        break
        case '4':     
        wx.navigateTo({
          url: '../../packageA/pages/user/calendars/calendars',
          })
          break
          case '5':     
          wx.navigateTo({
             url: '../../packageA/pages/games/gameInstruction/gameInstruction'      
            })
            break

            case '6':     
            wx.navigateTo({
              url: '../../packageA/pages/exercises/exercisesHome/exercisesHome?fromType=2',
              })
              break
    }
  },


  toExam: function (e) {
    wx.navigateToMiniProgram({
      appId: 'wx675db00d4f02fa1a',
      path: '/pages/index/index',
      envVersion: 'release',
      success(res) {
        // 打开成功
        wx.showToast({
          title: '跳转成功'
        })
      },
    })
  },

  getBanners: function() {
    var that = this;
    http.request(
      config.banner,
      'GET', null,
      function(res) {
        if (res.rows != null) {
          var array = []
          for (var i = 0; i < res.rows.length; i++) {
            array.push(res.rows[i].imgUrl)
          }
          that.setData({
            bannerData: array
          })
        }
      },
      function(err) {
        wx.showToast({
          title: '数据请求失败',
          icon: 'none',
        })
      }
    )
  },

})